package com.li.configserve.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import com.li.toolkit.constant.LogConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @Description： 初始化公共的JdbcTemplate
 * @Author：LiDong
 * @Create：2020/8/18
 * @Version：1.0.0
 */
@Configuration
public class JdbcTemplateConfig {

    private final Logger logger= LoggerFactory.getLogger(JdbcTemplate.class);

    /**
     * 注入DataSource
     */
    @Autowired
    DruidConfig druidConfig;

    @Bean
    public JdbcTemplate initJdbcTemplate(){
         try{
             DruidDataSource druidDataSource = druidConfig.druidDataSource();
             JdbcTemplate jdbcTemplate=new JdbcTemplate(druidDataSource);
             List<Map<String, Object>> maps = jdbcTemplate.queryForList("select 1");
             logger.info(LogConst.LOG_SUCCESS_PREFIX +"JdbcTemplate初始化完成..."+ JSONObject.toJSONString(maps));
             return jdbcTemplate;
         }catch (Exception e){
             logger.error(LogConst.LOG_FAILURE_PREFIX+e.getMessage());
             return null;
         }
    }


}
